define(['appRoutes','service/lazyDependencyResolver'], function(config, lazyDependencyResolver) {
	
try {
    app = angular.module('app');
} catch(e)  {
	app = angular.module('app', ['ngCookies', 'ngRoute']);
}

	app.config( [
	             
        '$routeProvider',
        '$locationProvider',
        '$controllerProvider',
        '$compileProvider',
        '$filterProvider',
        '$provide',
        '$httpProvider',


        function($routeProvider, $locationProvider, $controllerProvider, $compileProvider, $filterProvider, $provide, $httpProvider) {
        	
        	app.lazy = {};

        	app.lazy.controller = $controllerProvider.register;
        	app.lazy.directive  = $compileProvider.directive;
        	app.lazy.filter     = $filterProvider.register;
        	app.lazy.factory    = $provide.factory;
        	app.lazy.service    = $provide.service;

            angular.forEach(config.routes, function(route, path) {
            	
                $routeProvider.when(
                    path, {
                        templateUrl:route.templateUrl, 
                        resolve:lazyDependencyResolver(config.defaultDependencies.concat(route.dependencies))
                    }
                )
            });

            $routeProvider.otherwise({redirectTo: '/index'});

            var interceptor = function($q, $location) {
                return {
                    'responseError': function(response) {
                        if(response.status === 401 || response.status === 403) {
                            $location.path('/index');
                            return $q.reject(response);
                        }
                        else {
                            return $q.reject(response);
                        }
                    }
                }
            };
            $httpProvider.interceptors.push(interceptor);
        }
    ]);

    return app;
});
